package com.example.concurrent;

/**
 * @author Kent HAN
 * @date 2021-11-28
 */
public class DeadLockDemo {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();

    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock1) {
                System.out.println(Thread.currentThread() + "got lock1");
//                try {
//                    Thread.sleep(500);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                System.out.println(Thread.currentThread() + "is waiting to get lock2");
                synchronized (lock2){
                    System.out.println(Thread.currentThread() + "got lock2");
                }
            }
        }).start();

        new Thread(() -> {
            synchronized (lock2) {
                System.out.println(Thread.currentThread() + "got lock2");
//                try {
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
                System.out.println(Thread.currentThread() + "is waiting to get lock1");
                synchronized (lock1) {
                    System.out.println(Thread.currentThread() + "got lock1");
                }
            }
        }).start();
    }
}
